package com.vonchenchen.process;

import android.app.Service;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.Surface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.vonchenchen.renderer.FileDataProvider;
import com.vonchenchen.renderer.FrameRenderer;
import com.vonchenchen.renderer.RendererCanvas;

/**
 * Created by lidede on 2021/6/29.
 */
public class RendererProcessService extends Service {

    private static final String TAG = "Renderer_Service";

    static {
        System.loadLibrary("renderer");
    }

    private HandlerThread mDrawThread;
    private Handler mHandler;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new RendererBinder();
    }

    class RendererBinder extends IRendererInterface.Stub{

        @Override
        public void Init(Surface surface) throws RemoteException {

            Log.i(TAG, "Init");

            mDrawThread = new HandlerThread("Draw_Thread");
            mDrawThread.start();

            mHandler = new Handler(mDrawThread.getLooper(), new Handler.Callback() {

                @Override
                public boolean handleMessage(@NonNull Message msg) {

                    if(msg.what == 0){

                        Surface surface = (Surface)msg.obj;
                        Canvas canvas = surface.lockCanvas(new Rect(0,0,100,100));
                        canvas.drawARGB(0xee, 0xee, 0xee, 0xee);
                        surface.unlockCanvasAndPost(canvas);
                    }

                    return false;
                }
            });

            Message msg = Message.obtain();
            msg.what = 0;
            msg.obj = surface;
            mHandler.sendMessage(msg);
        }
    }
}
